package com.easyctrl.fragment;

import java.util.ArrayList;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.TextView;

import com.easyctrl.iface.OnBackDown;
import com.easyctrl.iface.OnBaseFragmentList;
import com.easyctrl.ldy.activity.MainApplication;
import com.easyctrl.ldy.activity.R;
import com.easyctrl.ldy.domain.ModuleBean;
import com.easyctrl.ldy.domain.ModulePortBean;
import com.easyctrl.ldy.domain.TextDescription;
import com.easyctrl.ldy.domain.TimerBean;
import com.easyctrl.ldy.util.SaveSingleObject;
import com.easyctrl.ui.base.BaseActivity;

public class BindMList extends BaseFragmentListview implements OnBaseFragmentList, OnBackDown {

	private String Tag = BindMList.class.getSimpleName();
	
	private ArrayList<ModuleBean> beans;
	private ArrayList<TimerBean> selectTimer;

	private int fragmentType;
	public static final int TIMETYPE = 1;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setOnBaseFragmentList(this);
		activity.setOnBackDown(this);
		beans = MainApplication.moduleManager.findByType();
		MainApplication.bindMListAdapter.setModuls(beans);
		isBack = true;
	}

	public void setFragmentType(int fragmentType) {
		this.fragmentType = fragmentType;
	}

	@Override
	public ListAdapter onAdapter() {
		return MainApplication.bindMListAdapter;
	}
	

	@Override
	public void onItemListviewClick(AdapterView<?> parent, View view,
			int position, long id) {

		TextView num = (TextView) view.findViewById(R.id.num);
		TextView mianban = (TextView) view.findViewById(R.id.mianban);
		TextView model = (TextView) view.findViewById(R.id.model);
		TextView keyNum = (TextView) view.findViewById(R.id.keyNum);
		
		TextDescription description = new TextDescription();
		description.num = num.getText().toString();
		description.mianban = mianban.getText().toString();
		description.model = model.getText().toString();
		description.keyNum = keyNum.getText().toString();
		
		ModuleBean module = beans.get(position);
		SaveSingleObject.getInstance().setObject(module);
		SaveSingleObject.getInstance().setTextObject(description);
		BindKeyGridView bindKeyGridView = new BindKeyGridView();
		bindKeyGridView.setModuleID(module);
		bindKeyGridView.setNum(description.num);
		bindKeyGridView.setName(description.keyNum);
		bindKeyGridView.setModel(description.model);
		bindKeyGridView.setMianban(description.mianban);
		bindKeyGridView.setSelectBeans(MainApplication.modulePortAdapterSimple.getSelects());
		bindKeyGridView.setObject(object);
		if (selectTimer != null && selectTimer.size() > 0) {
			bindKeyGridView.setSelectTimerBean(selectTimer.get(0));
		}
		activity.pushFragments("setting", bindKeyGridView, true, false,R.id.setting_center);
	}

	@Override
	public void onClick(View v) {
		try {
			TextDescription description = (TextDescription)SaveSingleObject.getInstance().getTextObject();
			if(description != null){
				description.isListBack = true;
			}
			activity.popFragments(BaseActivity.SETTING, R.id.setting_center, true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private ArrayList<ModulePortBean> selects;

	public void setSelectBeans(ArrayList<ModulePortBean> selects) {
		this.selects = selects;
	}

	public void setSelectTimer(ArrayList<TimerBean> selectTimer) {
		this.selectTimer = selectTimer;
	}

	private boolean isBack = false;

	@Override
	public void onPause() {
		super.onPause();
		isBack = false;
	}

	@Override
	public void onResume() {
		super.onResume();
		isBack = true;
		MainApplication.userManager.setCurrentFragment(Tag);
	}

	private Object object;

	public void setObject(Object object) {
		this.object = object;
	}

	public Object getObject() {
		return object;
	}

	@Override
	public void onKeyDonw(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (isBack) {

//				if (fragmentType == TIMETYPE) {
//					MainApplication.userManager
//							.setCurrentFragment("BindFragment");
//				}
				activity.popFragments(BaseActivity.SETTING,
						R.id.setting_center, true);
			}
		}
	}
}
